import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col, Table} from 'antd';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import routes from './routes';
import {TABLE} from '../constants';
import {techShipmentPartSource, techShipmentStockDetailStauts, techShipmentCommitment} from '../Enum';
import {conventEnumValueToString, formatAmount} from '../utils';
const {Description} = DescriptionList;
import {injectIntl} from './intl';
export class DetailPanel extends React.PureComponent {
    componentDidMount() {
        this.props.init();
    }

    render() {
        const {formatMessage} = this.props.intl;
        const colums = [{
            title: formatMessage({
                id: 'detailPanel.column.claimCode',
                defaultMessage: '索赔单号'
            }),
            dataIndex: 'claimCode',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.status',
                defaultMessage: '使用状态'
            }),
            dataIndex: 'status',
            render: text => conventEnumValueToString(techShipmentStockDetailStauts, text)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.qty',
                defaultMessage: '使用数量'
            }),
            dataIndex: 'qty',
        }];
        return (
            <div className="detail">
                <Card loading={this.props.loading}>
                    <DescriptionList size="large" title={<Row><Col span={6}>{formatMessage({
                        id: 'detailPanel.text.title',
                        defaultMessage: '基本信息'
                    })}</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.code',
                            defaultMessage: '编号'
                        })}>{this.props.detail.code}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.brandCode',
                            defaultMessage: '品牌'
                        })}>{`(${this.props.detail.brandCode})/${this.props.detail.brandName}`}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.dealerCode',
                            defaultMessage: '服务站编号'
                        })}>{this.props.detail.dealerCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.dealerName',
                            defaultMessage: '服务站名称'
                        })}>{this.props.detail.dealerName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.dutyUnitCode',
                            defaultMessage: '供应商编号'
                        })}>{this.props.detail.dutyUnitCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.dutyUnitName',
                            defaultMessage: '供应商名称'
                        })}>{this.props.detail.dutyUnitName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.productCategoryCode',
                            defaultMessage: '车型编号'
                        })}>{this.props.detail.productCategoryCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.partCode',
                            defaultMessage: '备件编号'
                        })}>{this.props.detail.partCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.partName',
                            defaultMessage: '备件名称'
                        })}>{this.props.detail.partName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.qty',
                            defaultMessage: '发运数量'
                        })}>{this.props.detail.qty}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.stockQty',
                            defaultMessage: '库存数量'
                        })}>{this.props.detail.stockQty}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.partSource',
                            defaultMessage: '备件来源'
                        })}>{conventEnumValueToString(techShipmentPartSource, this.props.detail.partSource)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.commitment',
                            defaultMessage: '费用承担方'
                        })}>{conventEnumValueToString(techShipmentCommitment, this.props.detail.commitment)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.shipmentType',
                            defaultMessage: '发运方式'
                        })}>{this.props.detail.shipmentType}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.shipmentBasis',
                            defaultMessage: '发件依据'
                        })}>{this.props.detail.shipmentBasis}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.shipmentFee',
                            defaultMessage: '运费'
                        })}>{formatAmount(this.props.detail.shipmentFee)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.customsClearanceFee',
                            defaultMessage: '清关费'
                        })}>{formatAmount(this.props.detail.customsClearanceFee)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.pickingCode',
                            defaultMessage: '领料单号'
                        })}>{this.props.detail.pickingCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.addressee',
                            defaultMessage: '收件人'
                        })}>{this.props.detail.addressee}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.trustees',
                            defaultMessage: '经办人'
                        })}>{this.props.detail.trustees}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.remark',
                            defaultMessage: '备注'
                        })}>{this.props.detail.remark}</Description>
                    </DescriptionList>
                </Card>
                <Card>
                    <DescriptionList title={formatMessage({
                        id: 'detailPanel.table.stockDetails',
                        defaultMessage: '库存使用清单'
                    })}>
                        <Table
                            className="white-space-nowrap"
                            columns={colums}
                            rowKey="id"
                            pagination={false}
                            dataSource={this.props.detail.stockDetails}
                            {...TABLE} />
                    </DescriptionList>
                </Card>

                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.props.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'detailPage.text.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    detail: PropTypes.object.isRequired,
    id: PropTypes.string.isRequired,
    init: PropTypes.func.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {
    getDetail,
    tableSearch
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getDetailData = selectorFactory(['page', 'domainData', 'detail', 'data']);


const mapStateToProps = (state, props) => ({
    id: props.id,
    loading: state.getIn(['page', 'domainData', 'detail', 'isFetching']),
    detail: getDetailData(state),
    permissions: getPermissions(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    init: () => dispatch(getDetail(props.id)),
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
